package com.example.hzq.food.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.hzq.food.R;
import com.example.hzq.food.activity.Login;
import com.example.hzq.food.adapter.Orderadapter;
import com.example.hzq.food.bean.OrderBean;
import com.example.hzq.food.model.Model;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.food.activity.Login.userid;

/**
 * Created by hzq on 2017/6/28.
 */

public class Fragment3 extends Fragment {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager manager;
    private Orderadapter orderadapter;
    private List<OrderBean> list=null;
    private View view=null;
    private Context context;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment3,container,false);
        context=getActivity();
        recyclerView=(RecyclerView)view.findViewById(R.id.order);
        manager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        orderadapter=new Orderadapter(context);
        orderadapter.setList(list);
        recyclerView.setAdapter(orderadapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        Model model=new Model();
        Call<List<OrderBean>> call= model.getorder(Integer.valueOf(userid));
        call.enqueue(new Callback<List<OrderBean>>() {
            @Override
            public void onResponse(Call<List<OrderBean>> call, Response<List<OrderBean>> response) {
                if(response.isSuccessful()&&response.body()!=null)
                {
                    list=response.body();
                    orderadapter.setList(list);
                }
                else
                {
                    Toast.makeText(context,"获取资源失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<OrderBean>> call, Throwable t) {
                Toast.makeText(context,"请检查网络连接",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
}
